#include"Stack.h"

//扩容
void AddCapacity(Stack* ps)
{
	assert(ps);
	if (ps->_capacity==0)
	{
		ps->_capacity = 4;
	}
	else
	{
		ps->_capacity = ps->_capacity * 2;
	}
	ps->_a = (STDataType*)realloc(ps->_a,sizeof(STDataType)*ps->_capacity);
}

//初始化栈
void StackInit(Stack* ps)
{
	assert(ps);
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = -1;
}

//入栈
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->_capacity == ps->_top + 1||ps->_capacity==0)
		AddCapacity(ps);
	ps->_top++;
	ps->_a[ps->_top] = data;
	ps->_capacity++;
}

// 出栈 
void StackPop(Stack* ps)
{
	assert(ps);
	ps->_top--;
}

// 获取栈顶元素 
STDataType StackTop(Stack* ps) 
{
	return ps->_a[ps->_top];
}

// 获取栈中有效元素个数 
int StackSize(Stack* ps)
{
	return ps->_top;
}

// 检测栈是否为空，如果为空返回非零结果，如果不为空返回0 
int StackEmpty(Stack* ps)
{
	if (ps->_top == -1)
	{
		return 0;
	}
	else
	{
		return ps->_top;
	}
}

// 销毁栈 
void StackDestroy(Stack* ps)
{
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = -1;
}